var m;
(function(){
	function eleAddEvent(element,method,func,mode){
    if(document.addEventListener){
      element.addEventListener(method,func,mode);
    }else{
      element.attachEvent("on"+method,func,mode);
    }
  }
  function elesAddEvent(elements,method,func,mode){
    for(var i=0;i<elements.length;i++){
      eleAddEvent(elements[i],method,func,mode);
    }
  }
  function scrollEvent(e){
		if (!_this.control) {
			return 0;
		}
		var currentDir = _this.getDirection(e);
		if (currentDir===_this.direction) {
			_this.scrollCount+=1;
			_this.direction = currentDir;
		}else{
			_this.scrollCount=1;
			_this.direction = currentDir;
		}
		console.log(_this.direction);
		console.log(_this.scrollCount);
		_this.scrollFunc(_this.direction);
	}
	function Scroll(){
		this.partScrollAllow = false;
		this.partNum = 1;
		this.direction = 0;//这里1代表向上，-1代表向下
		this.scrollCount = 0;
		this.control = 1;
	}

	Scroll.prototype = {
		init:function(){
			this.attachWheel();
			
		},
		getDirection:function(event){
			var direction = 0;
			var e = event || window.event;
			var directNum = e.detail||e.wheelDelta;
			if (Math.abs(directNum) === 120) {
				direction = directNum>0? 1:(-1);
			}else{
				direction = directNum>0? (-1):1;
			}
			return direction;
		},
		attachWheel:function(){
			var _this = this;
			eleAddEvent(document,'mousewheel',function(e){
				if (!_this.control) {
					return 0;
				}
				var currentDir = _this.getDirection(e);
				if (currentDir===_this.direction) {
					_this.scrollCount+=1;
					_this.direction = currentDir;
				}else{
					_this.scrollCount=1;
					_this.direction = currentDir;
				}
				console.log(_this.direction);
				console.log(_this.scrollCount);
				_this.scrollFunc(_this.direction);
			});
			eleAddEvent(document,'DOMMouseScroll',function(e){
				if (!_this.control) {
					return 0;
				}
				var currentDir = _this.getDirection(e);
				if (currentDir===_this.direction) {
					_this.scrollCount+=1;
					_this.direction = currentDir;
				}else{
					_this.scrollCount=1;
					_this.direction = currentDir;
				}
				console.log(_this.direction);
				console.log(_this.scrollCount);
				_this.scrollFunc(_this.direction);
			});
			elesAddEvent(document.getElementsByClassName('dot'),'click',function(e){
				var targetClass = e.target.classList[1];
				var targetNum = parseInt(targetClass[targetClass.length-1]);
				console.log(targetNum);
				console.log(_this.partNum);

				if (targetNum === _this.partNum) {
					return 0;
				}else if(targetNum>_this.partNum){
					console.log('aaa');
					for (var i = _this.partNum; i < targetNum; i++) {
						console.log('aaa');
						_this.scrollOne(i,-1);
					}
					_this.partNum = targetNum;
				}else if(targetNum<_this.partNum){
					for (var i = targetNum; i < _this.partNum; i++) {
						_this.scrollOne(i+1,1);
					}
					_this.partNum = targetNum;
				}
				$('.dot').removeClass('active');
				$('.dot'+_this.partNum).addClass('active');
			})
		},
		scrollOne:function(partNum,direction){
			if (partNum<=6&&partNum>=1) {
				if (direction===1 && partNum !=1) {
					$('.part'+(partNum-1)).css({'transform':'translate3d(0px,'+0+'px,0px)'});
					this.partNum-=1;
				}else if(direction===-1&& partNum !=6){
					$('.part'+partNum).css({'transform':'translate3d(0px,'+"-100%"+',0px)'});
					this.partNum+=1;
				}
				return 0;
			}
		},
		controlHeader:function(partNum,direction){
			if (partNum===2 && direction===-1) {
				$('header').hide();
			}else if(partNum===1 && direction===1){
				$('header').show();
			}
		},
		scrollFunc:function(direction){
			
			if (this.scrollCount>=3) {
				this.partScrollAllow = true;
			}else{
				return 0;
			}
			if (this.partScrollAllow) {
				var _this = this;
				this.control = 0;
				this.scrollOne(this.partNum,direction);
				this.controlHeader(this.partNum,direction);
				setTimeout(function(){
					_this.control = 1;
					console.log("partNum: "+_this.partNum);
					if (_this.partNum>1) {
						$('.dots').fadeIn();
						$('.dot').removeClass('active');
						$('.dot'+_this.partNum).addClass('active');
					}else{
						$('.dots').fadeOut();
					}
				},1000);
				this.partScrollAllow = false;
				this.scrollCount = 0;
			}
		}
	};
	m= new Scroll();
	m.init();

})();